package com.geeksaga.service;

import java.util.StringTokenizer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.geeksaga.common.util.HangleParsing;
import com.geeksaga.common.util.KeyGenerator;
import com.geeksaga.common.util.StringUtils;
import com.geeksaga.dao.BbsDao;
import com.geeksaga.dao.SpecDao;
import com.geeksaga.dao.TagDao;
import com.geeksaga.dao.TagMapDao;
import com.geeksaga.domain.Bbs;
import com.geeksaga.domain.Spec;
import com.geeksaga.domain.Tag;
import com.geeksaga.domain.TagMap;

/**
 * @author newbie
 * @since 0.1
 */
@Service
public class TagMapServiceImpl implements TagMapService {
    
    @Autowired protected BbsDao bbsDao;
    @Autowired protected SpecDao specDao;
    @Autowired protected TagMapDao bbsTagDao;
    @Autowired protected TagDao tagDao;

    /* (non-Javadoc)
     * @see com.geeksaga.service.TagMapService#add(com.geeksaga.domain.Bbs)
     */
    public Tag add(Bbs bbs) throws DataAccessException {
        return add(bbs, false);
    }
    
    public Tag add(Spec spec) throws DataAccessException {
    	return add(spec, false);
    }
    
    /* (non-Javadoc)
     * @see com.geeksaga.service.TagMapService#add(com.geeksaga.domain.Bbs, boolean)
     */
    public Tag add(Bbs bbs, boolean updateBbs) throws DataAccessException {
        Tag resultTag = null;
        
        if (!StringUtils.isEmpty(bbs.getTagName())) {
            StringTokenizer st = new StringTokenizer(bbs.getTagName(), ",");
            int tagCnt = st.countTokens();
            
            boolean isFirst = true;
            while (st.hasMoreTokens()) {
                String t = st.nextToken().trim();
                
                Tag tag = new Tag(t, HangleParsing.parse(t));
                TagMap bbsTag = new TagMap(bbs.getSid());
                
                Tag findTag = tagDao.findByTagName(tag);
                
                if (findTag == null) {
                    String tagSid = KeyGenerator.generatorKey();
                    tag.setSid(tagSid);
                    bbsTag.setTagSid(tagSid);
                    
                    tagDao.add(tag);
                } else {
                    bbsTag.setTagSid(findTag.getSid());
                    tag.setSid(findTag.getSid());
                    
                    tagDao.updateCnt(tag);
                }
                
                if(!bbsTagDao.exists(bbsTag))
                    bbsTagDao.add(bbsTag);
                else
                    tagCnt = tagCnt - 1;
                
                if (isFirst) {
                    bbs.setTagSid(tag.getSid());
                    bbs.setTagName(tag.getTagName());
                    
                    resultTag = tag;
                    
                    isFirst = false;
                }
            }
            
            bbs.setTagCnt(String.valueOf(tagCnt));
            
            if(updateBbs && tagCnt > 0)
                bbsDao.updateCnt(bbs, Bbs.CNT_TYPE.TAG_PLUS_CNT);
        }
        
        return resultTag;
    }
    
    public Tag add(Spec spec, boolean updateBbs) throws DataAccessException {
    	Tag resultTag = null;
    	
    	if (!StringUtils.isEmpty(spec.getTagName())) {
    		StringTokenizer st = new StringTokenizer(spec.getTagName(), ",");
    		int tagCnt = st.countTokens();
    		
    		boolean isFirst = true;
    		while (st.hasMoreTokens()) {
    			String t = st.nextToken().trim();
    			
    			Tag tag = new Tag(t, HangleParsing.parse(t));
    			TagMap bbsTag = new TagMap(spec.getSid());
    			
    			Tag findTag = tagDao.findByTagName(tag);
    			
    			if (findTag == null) {
    				String tagSid = KeyGenerator.generatorKey();
    				tag.setSid(tagSid);
    				bbsTag.setTagSid(tagSid);
    				
    				tagDao.add(tag);
    			} else {
    				bbsTag.setTagSid(findTag.getSid());
    				tag.setSid(findTag.getSid());
    				
    				tagDao.updateCnt(tag);
    			}
    			
    			if(!bbsTagDao.exists(bbsTag))
    				bbsTagDao.add(bbsTag);
    			else
    				tagCnt = tagCnt - 1;
    			
    			if (isFirst) {
    				spec.setTagSid(tag.getSid());
    				spec.setTagName(tag.getTagName());
    				
    				resultTag = tag;
    				
    				isFirst = false;
    			}
    		}
    		
    		spec.setTagCnt(String.valueOf(tagCnt));
    		
    		if(updateBbs && tagCnt > 0)
    			specDao.updateCnt(spec, Spec.CNT_TYPE.TAG_PLUS_CNT);
    	}
    	
    	return resultTag;
    }
}